package org.sys.transporttrainsys.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.sys.transporttrainsys.entity.TrainingPlan;
import org.sys.transporttrainsys.repository.TrainingPlanRepository;

import java.util.List;

@Service
public class TrainingPlanService {

    @Autowired
    private TrainingPlanRepository trainingPlanRepository;

    public List<TrainingPlan> getAllTrainingPlans() {
        return trainingPlanRepository.findAll();
    }

    public TrainingPlan getTrainingPlanById(Long id) {
        return trainingPlanRepository.findById(id).orElse(null);
    }

    public TrainingPlan createTrainingPlan(TrainingPlan trainingPlan) {
        return trainingPlanRepository.save(trainingPlan);
    }

    public TrainingPlan updateTrainingPlan(Long id, TrainingPlan trainingPlan) {
        // 确保如果存在则更新
        if (trainingPlanRepository.existsById(id)) {
            trainingPlan.setId(id);
            return trainingPlanRepository.save(trainingPlan);
        }
        return null; // 或者抛出一个自定义异常
    }

    public void deleteTrainingPlan(Long id) {
        trainingPlanRepository.deleteById(id);
    }

    // 根据名称查找培训计划
    public List<TrainingPlan> findByName(String name) {
        return trainingPlanRepository.findByName(name);
    }


    // 根据类型查询
    public List<TrainingPlan> findByType(String type) {
        return trainingPlanRepository.findByType(type);
    }

    // 根据名称和类型联合查询
    public List<TrainingPlan> findByNameAndType(String name, String type) {
        return trainingPlanRepository.findByNameAndType(name, type);
    }
}